//
//  ParserNET.java
//  TopoManipApplet
//
//  Created by Thibaud PIQUET on 23/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
package src.convertisseur;

import java.util.*;
import java.io.*;
import java.lang.*;
import org.jdom.*;

/**
 *
 * @author Thibaud PIQUET
 */
public class ParserNET implements Runnable {

    File ftp; // File to parse
    File fileOut; // File to create
    Reader fr; // File Reader
    BufferedReader br; // Buffer Reader
    String line;
    String chaine;
    String[] res;
    String[] keyGraphNode = {"name", "status", "switch"};
    String[] keyGraphEdge = {"status", "linkType", "weight"};

    public ParserNET(File file, File fileOUT) throws IOException {
        this.ftp = file;
        this.fileOut = fileOUT;
        this.fr = new FileReader(this.ftp);
        this.br = new BufferedReader(this.fr);
    }

    public void run() {
        try {
            this.parseToXML();
        } catch (Exception e) {
            e.printStackTrace();
            Resume result = Resume.getInstance();
            result.add("ParserNET", "procedure run had some problems : " + e);
        }
    }

    public void parseToXML() throws IOException {

        // <graphml xmlns="http://graphml.graphdrawing.org/xmlns">
        // <graph edgedefault="undirected">

        Element graphml = new Element("graphml");
        Namespace nsGraphml = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns");
        graphml.setNamespace(nsGraphml);


        Element graph = new Element("graph");
        Attribute edgedefault = new Attribute("edgedefault", "undirected");
        graph.setAttribute(edgedefault);


        Element domain = new Element("domain");
        Element information = new Element("information");
        Element topology = new Element("topology");
        Element nodes = new Element("nodes");
        Element links = new Element("links");

        org.jdom.Document document = new Document(graphml);

        Resume result = Resume.getInstance();

        try {
            line = this.br.readLine();

            Element title = new Element("title");
            Element date = new Element("date");
            Element author = new Element("author");
            Element description = new Element("description");
            Date today = new Date();
            String day = "";
            String month = "";

            title.setText(fileOut.getName());
            if (today.getDate() < 10) {
                day = "0" + today.getDate();
            } else {
                day = today.getDate() + "";
            }
            if ((today.getMonth() + 1) < 10) {
                month = "0" + (today.getMonth() + 1);
            } else {
                month = (today.getMonth() + 1) + "";
            }
            date.setText((today.getYear() + 1900) + "-" + month + "-" + day);
            author.setText("File Generator");
            description.setText("File generated by ParserNET from this jApplet");

            information.addContent(title);
            information.addContent(date);
            information.addContent(author);
            information.addContent(description);

            Element node = new Element("node");
            Element link = new Element("link");


            String state = "";
            int counterLink = 0;

            while (line != null) {
                if (line != null) {
                    if (line.startsWith("*Vertices")) {
                        state = "node";
                    } else if (line.startsWith("*Arcs")) {
                        state = "linkSimplex";
                    } else if (line.startsWith("*Edges")) {
                        state = "linkDuplex";
                    } else {
                        //System.out.println("line ="+line.toString());
                        res = line.split(" ");
                        int j = 0;

                        /* to know how much information there is in a line */
                        for (int i = 0; i < res.length; i++) {
                            //System.out.println(res[i]);

                            if (!res[i].equals("")) {
                                j++;
                            }
                        }

                        String tmp[] = new String[j];
                        //System.out.println("j="+j);
                        j = 0;

                        /* we fill the tab tmp */
                        for (int i = 0; i < res.length; i++) {
                            if (!res[i].equals("")) {
                                tmp[j] = res[i];
                                j++;
                            }
                        }

                        if (state.equals("node")) {
                            node = new Element("node");
                            Attribute idNode = new Attribute("id", tmp[0]); //
                            node.setAttribute(idNode);

                            Element NodeDescription = null;
                            if (tmp.length > 1) {
                                 NodeDescription = Utils.getNameNode("name", tmp[1].replace("\"", ""));
                            }else{
                                NodeDescription = Utils.getNameNode("name", tmp[1].replace("\"", ""));
                            }
                            node.addContent(NodeDescription);

                            Element statusNode = Utils.getNameNode("status", "UP");
                            Element switchNode = Utils.getNameNode("switch", "false");


                            node.addContent(statusNode);
                            node.addContent(switchNode);

                            nodes.addContent(node);
                        } else if (state.equals("linkSimplex")) {
                            counterLink++;
                            link = new Element("edge");

                            Attribute idLink = new Attribute("id", "" + counterLink);
                            link.setAttribute(idLink);

                            //Element source = new Element("source");
                            Attribute srcNode = new Attribute("source", tmp[0]);

                            link.setAttribute(srcNode);
                            //link.addContent(source);

                            //Element target = new Element("target");
                            Attribute tgtNode = new Attribute("target", tmp[1]);

                            link.setAttribute(tgtNode);
                            //link.addContent(target);

                            Element statusLink = Utils.getNameNode("status", "UP");
                            link.addContent(statusLink);

                            Element linkType = Utils.getNameNode("linkType", "");
                            Attribute type1 = new Attribute("type1", "simplex");
                            linkType.setAttribute(type1);
                            link.addContent(linkType);

                            if (tmp.length > 2) {
                                Element weight = Utils.getNameNode("weight", tmp[2]);
                                link.addContent(weight);
                            }

                            links.addContent(link);
                        } else if (state.equals("linkDuplex")) {
                            counterLink++;
                            link = new Element("edge");

                            Attribute idLink = new Attribute("id", "" + counterLink);
                            link.setAttribute(idLink);

                            //Element source = new Element("source");
                            Attribute srcNode = new Attribute("source", tmp[0]); //

                            link.setAttribute(srcNode);
                            //link.addContent(source);

                           // Element target = new Element("target");
                            Attribute tgtNode = new Attribute("target", tmp[1]); //

                            link.setAttribute(tgtNode);
                            //link.addContent(target);

                            Element statusLink = Utils.getNameNode("status", "UP");
                            link.addContent(statusLink);

                            Element linkType = Utils.getNameNode("linkType", "");
                            Attribute type1 = new Attribute("type1", "duplex");
                            linkType.setAttribute(type1);
                            link.addContent(linkType);

                            if (tmp.length > 2) {
                                Element weight = Utils.getNameNode("weight", tmp[2]);
                                link.addContent(weight);
                            }

                            links.addContent(link);
                        }
                    }
                }
                line = this.br.readLine();
            }
            br.close();
            fr.close();

            for (int i = 0; i < keyGraphNode.length; i++) {
                topology.addContent(Utils.getKeyGraph(i, keyGraphNode, "node"));

            }

            for (int i = 0; i < keyGraphEdge.length; i++) {
                topology.addContent(Utils.getKeyGraph(i, keyGraphEdge, "edge"));

            }


            topology.addContent(nodes);
            topology.addContent(links);

            domain.addContent(information);
            domain.addContent(topology);

            graph.addContent(domain);
            graphml.addContent(graph);

            org.jdom.output.XMLOutputter sortie = new org.jdom.output.XMLOutputter(org.jdom.output.Format.getPrettyFormat());
            sortie.output(document, new FileOutputStream(fileOut.getAbsolutePath()));

            result.add("ParserNET", "Import succeeded");

        } catch (Exception e) {
            e.printStackTrace();
            result.add("ParserNET", "Import had problems " + e);
        }
    }
}
